#include <odbc++/drivermanager.h>
#include <odbc++/connection.h>
#include <odbc++/resultset.h>
#include <odbc++/resultsetmetadata.h>
#include <odbc++/preparedstatement.h>

#include <syslog.h>

class	SQLData {
	odbc::Connection *con_;
	pthread_mutex_t SqlWriteMutex_;
	std::string	connectionInfo_;

	odbc::PreparedStatement*	iicStmt_;
	odbc::PreparedStatement*	sdnsStmt_;
	odbc::PreparedStatement*	pinsertStmt_;
	bool	connected_;
public:
	void	InitPreparedStatements();
	bool	Connect() {
		syslog(LOG_MAKEPRI(LOG_DAEMON, LOG_INFO), "Connecting to %s", connectionInfo_.c_str());
		try {
			con_ = odbc::DriverManager::getConnection(connectionInfo_);
		} catch(odbc::SQLException& /*e*/) { 
			syslog(LOG_MAKEPRI(LOG_DAEMON, LOG_INFO), "Failed (will try again later)");
			return false;
		}
		InitPreparedStatements();
		connected_ = true;
		return true;
	}
	SQLData(std::string connectionInfo) : con_(0), connectionInfo_(connectionInfo), connected_(false) {
		pthread_mutex_init(&SqlWriteMutex_, NULL);
		Connect();
	}
	void	Disconnect() {
//		odbc::DriverManager::shutdown();
		connected_ = false;
	}
	bool	SaveRecord(class CRecord *cr, std::string interfaceName,  struct timeval *now);
	int	GetDNS(std::string resolvedName, const struct in_addr& ip, const char *mac);
};
